Spring xml学习 发表于 2020-04-02 更新于 2020-04-07 分类于 Spring 基础 阅读次数: Spring xml 的一些基本配置。 12345678910111213141516171819202122232425262728293031<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean 需要有相对应的setter方法--> <bean id="helloWord" class="HelloWord.HelloWord"> <property name="name" value="Spring"></property> </bean> <!-- 配置单独的bean --> <bean id="person" class="HelloWord.Person"> <!-- 使用构造器来填充数据,需按照属性顺序 --> <constructor-arg value="小丽" ></constructor-arg> <constructor-arg value="女"></constructor-arg> <constructor-arg value="45000"></constructor-arg> <!-- 空值填充 --> <constructor-arg><null/></constructor-arg> </bean> <bean id="student" class="HelloWord.Student"> <property name="name" value="张三 "></property> <property name="sex" value="男 "></property> <property name="birthday" value="2019-1-2"></property> <!-- 使用ref进行调用定义的bean --> <property name="person" ref="person"></property> </bean> <!-- 使用自动装配(根据名字进行自动装配) 这里还使用了P命名空间(需开启)--> <bean id="test" class="List.Test" p:name="测试" autowire="byName"></bean></beans> 123456789101112131415161718192021222324252627282930<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 测试配置的继承 --> <!-- 可以使用bean的 abstract="true" 属性来设置抽象bean,不能被IOC容器实例化 只用来被继承配置--> <!-- 如果没有指定class 必须将Bean设定为抽象Bean --> <bean id="address" class="List.Address" p:address="重庆" p:street="万盛"></bean> <bean id="address2" class="List.Address" p:street="渝北" parent="address"></bean> <!-- 使用bean 的scope 属性来设置bean 的作用域 singleton:默认值,容器初始时创建bean 实例,在整个容器的生命周期内只创建这一个bean,单例的。 prototype:原型的,容器初始化时不创建bean 的实例,而在每次请求时都创建一个新的bean 实例并返回。 --> <bean id="Person" class="HelloWord.Person" scope="prototype"></bean> <!-- 外部文件 --> <context:property-placeholder location="classpath:pro.properties"/> <bean id="jdbc" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 使用外部文件的属性 --> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="driverClass" value="${driver}"></property> </bean> </beans> 12345678910111213141516171819202122232425262728293031package List;import java.sql.SQLException;import javax.sql.DataSource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) throws SQLException { //1.创建Spring的IOC 容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC 容器中获取Bean 示例 Test test = (Test) ctx.getBean("test"); System.out.println(test); ApplicationContext ctx1 = new ClassPathXmlApplicationContext("Student2.xml"); //2.从IOC 容器中获取Bean 示例 Address test1 = (Address) ctx1.getBean("address"); System.out.println(test1); test1 = (Address) ctx.getBean("address2"); System.out.println(test); ApplicationContext ctx2 = new ClassPathXmlApplicationContext("Student2.xml"); DataSource dataSource = (DataSource) ctx2.getBean("jdbc"); System.out.println(dataSource.getConnection()); }} 123456789101112131415161718192021222324252627282930313233343536<!-- 测试 SpEL: 可以为属性进行动态的赋值(了解) --> <bean id="girl" class="com.atguigu.spring.helloworld.User"> <property name="userName" value="周迅"></property> </bean> <bean id="boy" class="com.atguigu.spring.helloworld.User" init-method="init" destroy-method="destroy"> <property name="userName" value="高胜远"></property> <property name="wifeName" value="#{girl.userName}"></property> </bean> <!-- 配置 bean 后置处理器: 不需要配置 id 属性, IOC 容器会识别到他是一个 bean 后置处理器, 并调用其方法 --> <bean class="com.atguigu.spring.ref.MyBeanPostProcessor"></bean> <!-- 通过工厂方法的方式来配置 bean --> <!-- 1. 通过静态工厂方法: 一个类中有一个静态方法, 可以返回一个类的实例(了解) --> <!-- 在 class 中指定静态工厂方法的全类名, 在 factory-method 中指定静态工厂方法的方法名 --> <bean id="dateFormat" class="java.text.DateFormat" factory-method="getDateInstance"> <!-- 可以通过 constructor-arg 子节点为静态工厂方法指定参数 --> <constructor-arg value="2"></constructor-arg> </bean> <!-- 2. 实例工厂方法: 先需要创建工厂对象, 再调用工厂的非静态方法返回实例(了解) --> <!-- ①. 创建工厂对应的 bean --> <bean id="simpleDateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd hh:mm:ss"></constructor-arg> </bean> <!-- ②. 有实例工厂方法来创建 bean 实例 --> <!-- factory-bean 指向工厂 bean, factory-method 指定工厂方法(了解) --> <bean id="datetime" factory-bean="simpleDateFormat" factory-method="parse"> <!-- 通过 constructor-arg 执行调用工厂方法需要传入的参数 --> <constructor-arg value="1990-12-12 12:12:12"></constructor-arg> </bean> <!-- 配置通过 FactroyBean 的方式来创建 bean 的实例(了解) --> <bean id="user" class="com.atguigu.spring.ref.UserBean"></bean> 原创技术分享,您的支持将鼓励我继续创作 打赏 微信支付 支付宝